<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">用户昵称</label>
        <el-input
          v-model="query.nickName"
          clearable
          placeholder="用户名"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />

        <label class="el-form-item-label">用户ID</label>
        <el-input
          v-model="query.id"
          clearable
          placeholder="用户ID"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">手机号</label>
        <el-input
          v-model="query.phone"
          clearable
          placeholder="手机号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">推荐人ID</label>
        <el-input
          v-model="query.pid"
          clearable
          placeholder="推荐人ID"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!--       <label class="el-form-item-label">是否实名</label>
        <el-input
          v-model="query.realName"
          clearable
          placeholder="是否实名，1实名了，0未实名"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />-->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :visible.sync="dialog"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="推荐人Id">
            <el-input v-model="form.pid" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickName" style="width: 370px;" />
          </el-form-item>
          <!--         <el-form-item label="性别">
            <el-radio v-for="item in dict.sex" :key="item.id" v-model="form.sex" :label="item.value">{{ item.label }}
            </el-radio>
          </el-form-item>-->
          <!--<el-form-item label="生日">
            <el-input v-model="form.birthday" style="width: 370px;"/>
          </el-form-item>-->
          <!--<el-form-item label="城市">
            <el-input v-model="form.city" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="职业">
            <el-input v-model="form.occupation" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="个签">
            <el-input v-model="form.sign" style="width: 370px;"/>
          </el-form-item>-->
          <!--<el-form-item label="用户等级">
            <el-select v-model="form.level" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.app_user_level"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>-->
          <el-form-item label="用户类型">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.app_user_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否实名">
            <el-radio v-model="form.realName" :label="true">实名(认证)</el-radio>
            <el-radio v-model="form.realName" :label="false">未实名(未认证)</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <!--<el-button type="text">取消</el-button>-->
          <el-button type="primary" @click="comfirm">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" width="80" />
        <el-table-column prop="imgUrl" label="头像地址" width="80">
          <template slot-scope="scope">
            <el-avatar :size="50" :src="scope.row.imgUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="昵称" width="120" />
        <el-table-column prop="referralCode" label="推荐码" width="80" />
        <el-table-column prop="pid" label="推荐人ID" width="80" />
        <el-table-column prop="phone" label="手机号" width="160" />
        <!--      <el-table-column prop="sex" label="性别" width="50">
          <template slot-scope="scope">
            {{ dict.label.sex[scope.row.sex] }}
          </template>
        </el-table-column>-->
        <!--        <el-table-column prop="birthday" label="生日" />
                <el-table-column prop="city" label="城市" />-->
        <!--        <el-table-column prop="occupation" label="职业" />-->
        <!--        <el-table-column prop="sign" label="个签" />-->
        <el-table-column prop="type" label="用户类型" width="80">
          <template slot-scope="scope">
            {{ dict.label.app_user_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="是否实名" width="80">
          <template slot-scope="scope">
            {{ dict.label.app_real[scope.row.realName] }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="用户状态" width="80">
          <template slot-scope="scope">
            {{ dict.label.app_user_status[scope.row.state] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="注册时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="lastLoginTime" label="上次登陆时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastLoginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','ycUser:edit','ycUser:del']"
          fixed="right"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <udOperation
               :data="scope.row"
               :permission="permission"
             />-->
            <el-button type="primary" @click="editAuth(scope.row)">编辑</el-button>
            <el-button v-if="!scope.row.realName" type="success" @click="addAuth(scope.row.id)">认证</el-button>
          <!--  <el-button v-else type="danger" @click="delAuth(scope.row.id)">撤销</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudYcUser from '@/api/user/user'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  username: null,
  password: null,
  phone: null,
  imgUrl: null,
  pid: null,
  state: null,
  createTime: null,
  lastUpdateTime: null,
  nickName: null,
  sex: null,
  birthday: null,
  city: null,
  occupation: null,
  sign: null,
  lastLoginTime: null,
  level: null,
  type: null,
  realName: null
}
export default {
  name: 'YcUser',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['app_user_status', 'sex', 'app_user_level', 'app_user_type', 'app_real'],
  cruds() {
    return CRUD({ title: '用户', url: 'api/user', sort: 'id,desc', crudMethod: { ...crudYcUser }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'ycUser:add'],
        edit: ['admin', 'ycUser:edit'],
        del: ['admin', 'ycUser:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'username', display_name: '用户名' },
        { key: 'phone', display_name: '手机号' },
        { key: 'pid', display_name: '推荐人Id' },
        { key: 'state', display_name: '0正常1删除2冻结' },
        { key: 'level', display_name: '用户等级 type为1的时候有值 1普通用户、2会员用户' },
        { key: 'type', display_name: '1店铺用户、2司机、3业务员' },
        { key: 'realName', display_name: '是否实名，1实名了，0未实名' }
      ],
      dialog: false,
      authinfo: {},
      form: {
        userId: '',
        pid: '',
        state: '',
        type: '',
        level: '',
        realName: ''
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.query.realName = null
      console.log(this.query)
      this.crud.optShow = {
        add: false,
        edit: false,
        del: false,
        download: true
      }
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      console.log(this.crud.data)
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      console.log(this.form)
      return true
    },
    addAuth(userId) {
      console.log(this.form)
      this.form.userId = userId
      this.form.realName = true
      crudYcUser.editAuth(this.form).then(res => {
        this.crud.refresh()
      })
    },
    delAuth(userId) {
      crudYcUser.editAuth(userId, false).then(res => {
        this.crud.refresh()
      })
    },
    getAuth(rows) {
      const quary = {
        id: '100030'
      }
      crudYcUser.getAuth(rows).then(res => {
        console.log(res)
        if (res.content.length > 0) {
          console.log(res.content[0])
          this.form = res.content[0]
          this.form.userId = res.content[0].id
        } else {
          this.$message.error('提示信息：未找到该用户信息')
        }
      })
    },
    editAuth(rows) {
      console.log(rows)
      this.getAuth(rows)
      this.dialog = true
    },
    comfirm() {
      console.log(this.form)
      crudYcUser.editAuth(this.form).then(res => {
        console.log(res)
        this.dialog = false
        this.crud.refresh()
        this.$message({
          message: '提示信息：成功修改用户信息',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
